<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  ajax_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-18.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Ajax_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->userinfo = $this->session->userdata('userinfo');
			$this->load->model('Comment_model');
			$this->load->model('Collection_model');
		}
		
		function get_comments($aid){
			$data = $comments = array();
			$comments = $this->Comment_model->get_comments($aid);
			foreach ($comments as $comment) {
				$comment['dateline'] = sgmdate("Y-m-d H:i", $comment['dateline'], 1);
				$data[] = $comment;
			}
			return $data;
		}
		//删除评论
		function add_comment($comments){
			if ($comments) {
				$insert_id = $this->Comment_model->insert_comment($comments);
				// 评论数
				if ($comments['aid']) {
					$aid = intval($comments['aid']);
					$this->db->query("UPDATE ".$this->db->dbprefix('activity')." SET replynum = replynum + 1 WHERE aid='".$aid."'");
				}

				return $insert_id;
			}
			return false;

		}
		//删除评论
		function del_comment($cid){
			if ($cid) {
				$comment = $this->Comment_model->get_comment_by_cid($cid);
				if ($comment['uid'] == $this->userinfo['uid']) {
					$result = $this->Comment_model->delete_comment($cid);
					if ($comment['aid']) {
						$aid = intval($comment['aid']);
						$this->db->query("UPDATE ".$this->db->dbprefix('activity')." SET replynum = replynum - 1 WHERE aid='".$aid."'");
					}
					return '1';
				}else{
					return '2';
				}
			}
			return '3';
		}
		function add_collection($collection){
			if ($collection) {
				$mycollection = $this->Collection_model->get_Collection_by_aid($collection['aid']);
				if ($mycollection) {
					$data = array(
							'code' => '2',
							'collection' => $collection
						);
					return $data;
				}
				$insert_id = $this->Collection_model->insert_collection($collection);
				$data = array(
						'code' => '1',
						'insert_id' => $insert_id
					);
				return $data;
			}
			return false;
		}
		
		function del_collection($aid){
			$uid = $this->userinfo['uid'] ? $this->userinfo['uid'] : '';
			if (!$uid) {
				$data = array(
						'code' => '2',
					);
				return $data;
			}
			if ($aid) {
				$result = $this->Collection_model->delete_collection($aid);
				if ($result) {
					$data = array(
							'code' => '1',
						);
					return $data;
				}else{
					$data = array(
							'code' => '4',
						);
					return $data;					
				}
			}else{
				$data = array(
						'code' => '3',
					);
				return $data;				
			}

		}
}
?>